package com.bto.solar.work.service;


import com.bto.solar.work.common.BizException;
import com.bto.solar.work.constant.BizCode;
import com.bto.solar.work.constant.DBConstants;
import com.bto.solar.work.dao.IPermissionsService;
import com.bto.solar.work.entity.Permissions;
import com.bto.solar.work.entity.User;
import com.bto.solar.work.utils.JsonToListUtils;
import com.bto.solar.work.utils.NullUtils;
import com.bto.solar.work.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;


@Service
@Slf4j
public class BtoPermissionService {

    @Autowired
    private IPermissionsService permissionsService;
    @Autowired
    @Lazy
    private BtoUserService btoUserService;


    /**
     * 根据 用户Id 查询 用户可创建业务权限的name
     */
    public List<String> getOrderCategoryByUserId(Long userId) {
        //判断userId是否为空
        if (NullUtils.isNull(userId)) {
            throw new BizException(BizCode.PARAM_ERROR);
        }
        User user = btoUserService.getUserByUserId(userId);
        if (NullUtils.isEmpty(user.getPermissionIds())) {
            return new ArrayList<>();
        }
        List<String> permissionIds = JsonToListUtils.convertJSONArrayToStringList(user.getPermissionIds());
        return permissionsService.lambdaQuery()
                .in(Permissions::getName, permissionIds)
                .eq(Permissions::getGuardName, DBConstants.CREATE_ORDER_CATEGORY_GROUP)
                .list().stream().map(Permissions::getName).collect(Collectors.toList());
    }

    /**
     * 获取用户查询可创建业务条件数据(用户管理-查询框-可创建业务权限类型)
     */
    public List<UserVO.Permissions> getOrderCategoryByUserQuery() {
        User loginUser = btoUserService.getCurrentUser();
        if (NullUtils.isNull(loginUser)) {
            throw new BizException(BizCode.USER_ABSENT_ERROR);
        }
        //判断是否为超级管理员
        if (btoUserService.isUserSuperAdmin(loginUser)) {
            //返回所有的可创建业务
            return permissionsService.lambdaQuery()
                    .eq(Permissions::getGuardName, DBConstants.CREATE_ORDER_CATEGORY_GROUP)
                    .list().stream().map(permissions -> {
                        UserVO.Permissions userPermissions = new UserVO.Permissions();
                        userPermissions.setName(permissions.getName()).setChineseName(permissions.getChineseName());
                        return userPermissions;
                    }).collect(Collectors.toList());
        }
        //不是超姐管理员，只能显示自己可创建业务权限的数据
        else {
            List<String> permissionIds = JsonToListUtils.convertJSONArrayToStringList(loginUser.getPermissionIds());
            return permissionsService.lambdaQuery()
                    .in(Permissions::getName, permissionIds)
                    .eq(Permissions::getGuardName, DBConstants.CREATE_ORDER_CATEGORY_GROUP)
                    .list().stream().map(permissions -> {
                        UserVO.Permissions userPermissions = new UserVO.Permissions();
                        userPermissions.setName(permissions.getName()).setChineseName(permissions.getChineseName());
                        return userPermissions;
                    }).collect(Collectors.toList());
        }
    }



    /**
     * 根据 用户Id 和 可创建业务权限id 判断用户是否可创建该业务权限
     */
    public boolean decideOrderCategoryByUserId(Long userId, String permissionName) {
        //判断参数是否为空
        if (NullUtils.isNull(userId) && NullUtils.isNull(permissionName)) {
            throw new BizException(BizCode.PARAM_ERROR);
        }
        //根据用户Id查询用户可创建业务
        List<String> orderCategories = getOrderCategoryByUserId(userId);
        //判断用户是否可创建该业务
        return orderCategories.contains(permissionName);
    }


    /**
     * 通过userId获取用户权限列表
     */
    public List<Permissions> getPermissionListByUserId(Long userId) {
        //获取用户信息
        User user = btoUserService.getUserByUserId(userId);
        //判断用户是否存在
        if (user == null) {
            throw new BizException(BizCode.USER_ABSENT_ERROR);
        }
        return getPermissionListByUser(user);
    }


    /**
     * 通过user获取用户权限列表
     */
    public List<Permissions> getPermissionListByUser(User user) {
        List<String> permissionIds = JsonToListUtils.convertJSONArrayToStringList(user.getPermissionIds());
        if(NullUtils.isEmpty(permissionIds)){
            Collections.emptyList();
        }
        return permissionsService.lambdaQuery().in(Permissions::getName, permissionIds).list();
    }

    /**
     * 获取权限列表
     */
    public List<UserVO.Permissions> getPermissionList() {
        return permissionsService.lambdaQuery()
                .list().stream().map(
                        permission -> {
                            UserVO.Permissions userVoPermission = new UserVO.Permissions();
                            userVoPermission.setName(permission.getName());
                            userVoPermission.setChineseName(permission.getChineseName());
                            return userVoPermission;
                        }
                ).collect(Collectors.toList());
    }


}
